package com.shanghaionstar.user;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.onstar.cn.auth.userProfile.Vehicle;
import com.onstar.cn.common.exception.OnStarException;

import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.more.response.VehicleImgRes;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.OnstarApplication;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.MoreActivity;
import com.shanghaionstar.user.adapter.ChooseVehicleImgAdapter;
import com.shanghaionstar.user.iview.IChooseVehicleImgView;
import com.shanghaionstar.user.presenter.ChooseVehicleImgPresenter;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.weight.TopLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mengzhou Gu on 2016/8/30.
 */
public class ChooseVehicleImgFragment extends BaseFragment implements IChooseVehicleImgView, ChooseVehicleImgDialog.DialogSaveListener {
    @FindViewById
    private TopLayout top_layout;
    @FindViewById(R.id.content_gv)
    GridView contentGv;

    @FindViewById(R.id.content_list_progress)
    private ProgressLayout          progressView;
    private MoreActivity            parentActivity;
    //  private CommonAdapter  mAdapter;
    private ChooseVehicleImgAdapter adapter;

    //  private ChooseVehicleImgDialog chooseCarDialog;

    private ImageLoader imageLoader;

    private VehicleImgRes res;
    //  private List<VehicleImgRes> mImgList=new ArrayList<>();
    private List<VehicleImgRes> myList = new ArrayList<>();
    private ChooseVehicleImgDialog chooseCarDialog;
    private ChooseVehicleImgPresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.vehicle_info_choose_car_fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        parentActivity = (MoreActivity) activity;

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        btnBackClick(top_layout);
        if (isFirstLoad()) {
            imageLoader = ImageLoader.getInstance();
            presenter = new ChooseVehicleImgPresenter(parentActivity.getScheduler());
            presenter.setView(this);
            presenter.start();
        }
    }

    @Override
    public void setLoading(boolean isLoading) {
        if (isLoading) {
            progressView.setVisibility(View.VISIBLE);
        } else {
            progressView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResponseError(OnStarException e) {
    }

    @Override
    public void onReponseSuccess(final List<VehicleImgRes> object) {
        if (object != null) {
            if (object.size() > 0) {
                if (adapter == null) {
                    //adapter=new ChooseVehicleImgAdapter(vehicleImgReses,parentActivity,imageLoader);
                    adapter = new ChooseVehicleImgAdapter(object, parentActivity, imageLoader);
                    contentGv.setAdapter(adapter);
                } else {
                    adapter.notifyDataSetChanged();
                }

            }
            chooseCarDialog = new ChooseVehicleImgDialog(parentActivity);
            chooseCarDialog.setDialogOccupListener(this);
            contentGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    VehicleImgRes res = object.get(position);
                    chooseCarDialog.showDia(res, imageLoader);
                }
            });
        }
    }

    @Override
    public Activity onActivity() {
        return getActivity();
    }

    @Override
    public void btnConfirmClick(VehicleImgRes res) {
        if (res == null)
            return;
        int imageID = res.getImageID();
        this.res = res;
        presenter.getData(imageID+"");
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        imageLoader.stop();
        // contentGv.removeAllViews();
        chooseCarDialog = null;
        contentGv = null;
        adapter = null;
        progressView.setVisibility(View.GONE);
        // progressView = null;
    }

    @Override
    public void saveSuccess(FeedBackResult feedBackResult) {
        if (feedBackResult != null) {
            if (GsonHelper.toJson(feedBackResult).contains("E0000")) {
                chooseCarDialog.dismiss();
                if (res != null) {
                    OnstarApplication application = (OnstarApplication) parentActivity
                            .getApplicationContext();
                    application.setRes(res);
                    Vehicle currentVehicle = ApplicationUtil.getInstance().getCurrentVehicle();
                    currentVehicle.setModel(res.getModelDesc());
                }
                AlertDialogUtil.showAlertDialog(parentActivity, parentActivity.getString(R.string.dialog_warning),
                        parentActivity.getString(R.string.option_switch_vehicle_success),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                back();
                            }
                        });
            }
        }
    }

    @Override
    public void saveFailure(Exception e) {
        chooseCarDialog.showFailedTips();
    }

    @Override
    public void saveLoading(boolean b) {
        if (b) {
            chooseCarDialog.showProgress();
        } else {
            chooseCarDialog.dismissProgress();
        }
    }
}

